package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpCarTireArchivesMapper;
import com.monitor.dao.ErpCustomerMapper;
import com.monitor.dao.ErpCustomerWarehouseMapper;
import com.monitor.dao.ErpInspectionContentMapper;
import com.monitor.dao.ErpMaterielMapper;
import com.monitor.dao.ErpModelMapper;
import com.monitor.dao.ErpSpecificationsMapper;
import com.monitor.dao.ErpSystemParametersChildMapper;
import com.monitor.dao.ErpTireArchivesMapper;
import com.monitor.dao.ErpWarehouseMapper;
import com.monitor.entity.*;
import com.monitor.service.ErpMaterielService;
import com.monitor.util.Result;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-08
 */
@Service
public class ErpMaterielServiceImpl extends ServiceImpl<ErpMaterielMapper, ErpMateriel> implements ErpMaterielService {
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpSpecificationsMapper erpSpecificationsMapper;
	
	@Resource
	private ErpModelMapper erpModelMapper;
	
	@Resource
	private ErpSystemParametersChildMapper erpSystemParametersChildMapper;
	
	@Resource
	private ErpCarTireArchivesMapper tireMapper;
	
	@Resource
	private ErpCustomerWarehouseMapper erpCustomerWarehouseMapper;
	
	@Resource
	private ErpWarehouseMapper erpWarehouseMapper;
	
	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpInspectionContentMapper erpInspectionContentMapper;
	
	@Resource
	private ErpTireArchivesMapper erpTireArchivesMapper;
	
	@Override
	public Result<List<MaterIelName>> yiHuangShi() {
		// TODO Auto-generated method stub
		List<ErpSpecifications> list=erpSpecificationsMapper.selectList(new EntityWrapper<ErpSpecifications>().eq("pid", "SF0"));
		List<MaterIelName> nameList=new ArrayList<>();
		for(ErpSpecifications materiel:list) {
			MaterIelName name=new MaterIelName();
			name.setMaterIelName(materiel.getName());
			List<Map<String, Object>> StrList0=erpSpecificationsMapper.selectByPid(materiel.getPid());
			for(Map<String, Object> name1:StrList0) {
				List<Map<String, Object>> StrList=erpSpecificationsMapper.selectByPid(name1.get("pid").toString());
				if(StrList==null||StrList.size()==0) {
					continue;
				}
				List<MaterielBrand> brandList=new ArrayList<>();
				for(Map<String, Object> brand:StrList) {
					MaterielBrand materielBrand=new MaterielBrand();
					materielBrand.setMaterielBrand(brand.get("name").toString());
					String pid=brand.get("pid").toString();
					List<MaterielSpecifications> specList=new ArrayList<>();
					List<Map<String, Object>> strList1=erpSpecificationsMapper.selectByPid(pid);
					if(strList1==null||strList1.size()==0) {
						continue;
					}
					for(Map<String, Object> str:strList1) {
						MaterielSpecifications spec=new MaterielSpecifications();
						spec.setMaterielSpecifications(str.get("name").toString());
						List<MaterielModel> modelList=new ArrayList<>();
						pid=str.get("pid").toString();
						List<Map<String, Object>> strList2=erpModelMapper.selectByPid(pid);
						if(strList2==null||strList2.size()==0) {
							continue;
						}
						for(Map<String, Object> mod:strList2) {
							MaterielModel model=new MaterielModel();
							model.setMaterielModel(mod.get("name").toString());
							pid=mod.get("pid").toString();
							List<Map<String, Object>> strList3=erpModelMapper.selectByPid(pid);
							if(strList3==null||strList3.size()==0) {
								continue;
							}
							List<MaterielPlace> placeList=new ArrayList<>();
							for(Map<String, Object> place:strList3) {
								MaterielPlace pla=new MaterielPlace();
								pla.setMaterielPlace(place.get("name").toString());
								List<String> strList4=erpSystemParametersChildMapper.selectStringListByPid(69);
								if(strList4==null||strList4.size()==0) {
									continue;
								}
								List<MaterielState> stateList=new ArrayList<>();
								for(String state:strList4) {
									MaterielState sta=new MaterielState();
									sta.setMaterielState(state);
									List<MaterielUnit> unitList=new ArrayList<>();
									List<String> strList5=erpSystemParametersChildMapper.selectStringListByPid(51);
									if(strList5==null||strList5.size()==0) {
										continue;
									}
									for(String unit:strList5) {
										MaterielUnit un=new MaterielUnit();
										un.setMaterielUnit(unit);
										List<String> strList6=erpSystemParametersChildMapper.selectStringListByPid(27);
										if(strList6==null||strList6.size()==0) {
											continue;
										}
										List<MaterielType> typeList=new ArrayList<>();
										for(String type:strList6) {
											MaterielType type1=new MaterielType();
											type1.setName(type);
											typeList.add(type1);
										}
										un.setTypeList(typeList);
										unitList.add(un);
									}
									sta.setUnitList(unitList);
									stateList.add(sta);
								}
								pla.setStateList(stateList);
								placeList.add(pla);
							}
							model.setPlaceList(placeList);
							modelList.add(model);
						}
						spec.setModelList(modelList);
						specList.add(spec);
					}
					materielBrand.setSpecificationsList(specList);
					brandList.add(materielBrand);
				}
				name.setBrandList(brandList);
				nameList.add(name);
			}
		}
		//List<Map<String, Object>> list=new ArrayList<>();
		
		return new Result<>(nameList,nameList.size());
	}

	@Override
	public Result<List<ErpSpecifications>> selectTop() {
		// TODO Auto-generated method stub
		List<ErpSpecifications> list=erpSpecificationsMapper.selectList(new EntityWrapper<ErpSpecifications>().eq("pid", "SF0"));
		return new Result<List<ErpSpecifications>>(list,list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> selectTwo(String pid) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> strList=erpSpecificationsMapper.selectByPid(pid);
		return new Result<List<Map<String,Object>>>(strList, strList.size());
	}

	@Override
	public Result<List<Map<String, Object>>> selectThree(String pid) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> strList=erpModelMapper.selectByPid(pid);
		return new Result<List<Map<String,Object>>>(strList, strList.size());
	}

	@Override
	public Result<List<String>> selectSytem(int shu) {
		// TODO Auto-generated method stub
		List<String> strList=erpSystemParametersChildMapper.selectStringListByPid(shu);
		return new Result<List<String>>(strList, strList.size());
	}

	@Override
	public Result<List<ErpMateriel>> selectByCarId(int carId) {
		// TODO Auto-generated method stub
		List<ErpCarTireArchives> carPage=tireMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", carId));
		List<ErpMateriel> list=new ArrayList<>();
		for(ErpCarTireArchives tire:carPage) {
			ErpMateriel mat=erpMaterielMapper.selectById(tire.getTireId());
			list.add(mat);
		}
		return new Result<List<ErpMateriel>>(list,list.size());
	}

	@Override
	public Result<List<String[]>> selectByBranch(List<Integer> warehouseIds, String startTime, String endTime,String name) {
		// TODO Auto-generated method stub
		List<ErpCustomerWarehouse> cus = erpCustomerWarehouseMapper.selectBatchIds(warehouseIds);
		List<String[]> newRetuDatas = new ArrayList<String[]>();
		String WarId = "-1";
		for(ErpCustomerWarehouse c:cus){
			WarId +=","+c.getId();
		}
		
		//未税单价：{原库存数量*原未税单价+新入库数量*无发票单价+新入库数量*增值税发票单价/1.17)/新原数量总和
		//含税单价：{原库存数量*原含税单价+新入库数量*增值税发票单价)/新原数量总和

		//期初物料
		String name1="";
		if(!name.equals("")) {
			name1=" AND cw.materiel_name = '"+name+"'";
		}
		List<Map<String, Object>> mfslistW = erpMaterielMapper.selectByWareIdsAndName(WarId, name1, startTime);

		// 出入库
		name1="";
		if(!name.equals("")) {
			name1="AND cw.materiel_name = '"+name+"' ";
		}
		List<Map<String, Object>> mfslist0W = erpMaterielMapper.selectByWareIdsAndTime(WarId, name1, startTime, endTime);

		DecimalFormat df = new DecimalFormat("######0.00");
		List<String[]> retuDatas = new ArrayList<String[]>();
		List<String> Fenleis = new ArrayList<String>();
		for (int i = 0; i < mfslistW.size(); i++) {         //生成期初数据   这里只是查询全部的功能 下面的循环是计算期初期末数量的作用
			String[] retuData = new String[17];
			Map<String, Object> QC =  mfslistW.get(i);             
			if (null != QC.get("materiel_name")) {
				String Pm = QC.get("materiel_name").toString();
				String Pp = QC.get("materiel_brand").toString();
				String Gg = QC.get("materiel_specifications").toString();
				String Xh = QC.get("materiel_model").toString();
				String Cd = QC.get("materiel_place").toString();
				String Dw = QC.get("materiel_unit") == null ? "" : QC.get("materiel_unit").toString();
				String ZT = QC.get("materiel_state").toString();
				String QCs = QC.get("num").toString();             //期初数量
				String YJ=QC.get("materiel_warn_amount").toString();         //预警值
				retuData[0] = QC.get("materiel_id").toString();
				retuData[1] = Pm;
				retuData[2] = Pp;
				retuData[3] = Gg;
				retuData[4] = Xh;
				retuData[5] = Cd;
				retuData[6] = ZT;
				int Qc = Integer.parseInt(QCs);
				retuData[7] = Qc < 0 ? "0" : QCs;
				retuData[8] = "0";
				retuData[9] = "0";
				retuData[10] = Qc < 0 ? "0" : QCs;
				retuData[11] = Dw;
				retuData[12] = QC.get("materiel_warn_amount") == null || QC.get("materiel_warn_amount").toString().equals("0") ? "" : QC.get("materiel_warn_amount").toString();
				retuData[13] = "0";
				retuData[14] = "0";
				retuData[15]=QC.get("warehouse_id").toString();
				retuData[16]=YJ;
				retuDatas.add(retuData);
				Fenleis.add(Pm + Pp + Gg + Xh + Cd + Dw + ZT);             //记录当前数据的品牌品名型号等数据
			}
		}

if(mfslistW!=null) {
		for (Map<String, Object> CRK:mfslist0W) {      //循环计算期末数量
			String Rk ="0";
			if(CRK.get("rk")!=null) {
				Rk= CRK.get("rk").toString();
			}
			
			String Ck = "0";
			if(CRK.get("ck")!=null) {
				Ck=CRK.get("ck").toString();
			}
			if (null != CRK.get("materiel_name")) {
				String Pm = CRK.get("materiel_name").toString();
				String Pp = CRK.get("materiel_brand").toString();
				String Gg = CRK.get("materiel_specifications").toString();
				String Xh = CRK.get("materiel_model").toString();
				String Cd = CRK.get("materiel_place").toString();
				String Dw = CRK.get("materiel_unit") == null || CRK.get("materiel_unit").toString().equals("0") ? "" : CRK.get("materiel_unit").toString();
				String ZT = CRK.get("materiel_state").toString();
				String YJ=CRK.get("materiel_warn_amount").toString();         //预警值
				String Fenlei = Pm + Pp + Gg + Xh + Cd + Dw + ZT;
				int index = Fenleis.indexOf(Fenlei);                   //找上个循环里是否有这个物料的数据
				if (index == -1) {                 //如果不含生成期初、期末都为0的数据
					String[] retuData = new String[17];
					retuData[0] = CRK.get("materiel_id").toString();
					retuData[1] = Pm;
					retuData[2] = Pp;
					retuData[3] = Gg;
					retuData[4] = Xh;
					retuData[5] = Cd;
					retuData[6] = ZT;
					retuData[7] = "0";
					retuData[8] = Rk;
					retuData[9] = Ck;
					int Qm = Integer.parseInt(Rk) - Integer.parseInt(Ck);
					retuData[10] = Qm < 0 ? "0" : Qm + "";
					retuData[11] = Dw;
					retuData[12] = CRK.get("materiel_warn_amount") == null || CRK.get("materiel_warn_amount").toString().equals("0") ? "" : CRK.get("materiel_warn_amount").toString();
					retuData[13] = "0";
					retuData[14] = "0";
					retuData[15]=CRK.get("warehouse_id").toString();
					retuData[16]=YJ;
					retuDatas.add(retuData);
				} else {                    //如果含 计算期初期末数量
					String[] retuData = retuDatas.get(index);
					retuData[8] = Rk;
					retuData[9] = Ck;
					retuData[10] = Integer.parseInt(retuData[7])
							+ Integer.parseInt(Rk) - Integer.parseInt(Ck) + "";            //计算期末数量
					retuData[12] = CRK.get("materiel_warn_amount") == null ? "" : CRK.get("materiel_warn_amount").toString();
					retuDatas.set(index, retuData);
				}
			}
		}

		
		
		for(int i = 0; i<retuDatas.size(); i++){          //循环计算含税单价和不含税单价
			String[] retuData = retuDatas.get(i);
			List<ErpWarehouse> ws = erpWarehouseMapper.selectByWareIds(retuData[1], retuData[2], retuData[3], retuData[4], retuData[5], retuData[6], retuData[11],WarId);
			Double materielTax = 0.00;
			Double materielNotax = 0.00;
			for(ErpWarehouse w:ws){
				materielTax += w.getMaterielTax();
				materielNotax += w.getMaterielNotax();
			}
			if(ws.size() > 0){
				retuData[13] = df.format(materielNotax/ws.size());
				retuData[14] = df.format(materielTax/ws.size());
			}else{
				retuData[13] = df.format(0.00);
				retuData[14] = df.format(0.00);
			}
			newRetuDatas.add(retuData);
		}
}
		return new Result<List<String[]>>(newRetuDatas, newRetuDatas.size());
	}

	@Override
	public Result<Map<String, Object>> findByLibraryName(int WareId, List<Integer> warehouseIds, String startTime,
			String endTime) {
		// TODO Auto-generated method stub
		ErpWarehouse w = erpWarehouseMapper.selectById(WareId);
		
		//查询客户和仓库
		ErpCustomer cus = erpCustomerMapper.selectById(w.getCustomerId());
		ErpCustomerWarehouse cw =erpCustomerWarehouseMapper.selectById(w.getWarehouseId());
		
		Map<String, Object> map=new HashMap<>();
		map.put("materielId", w.getId());
		map.put("wareId", cw.getId());
		map.put("cusName", cus.getCusName());                  
		map.put("warName", cw.getWarName());                    //仓库名 
		map.put("materielName", w.getMaterielName());				//品名
		map.put("materielBrand", w.getMaterielBrand());				//品牌
		map.put("materielSpecifications", w.getMaterielSpecifications());		//规格
		map.put("materielModel", w.getMaterielModel());				//物料 型号
		map.put("materielPlace", w.getMaterielPlace());				 //物料 产地
		map.put("materielState", w.getMaterielState());				//物料 状态
		map.put("materielUnit", w.getMaterielUnit());				//物料 单位
		map.put("materielNumber", w.getMaterielNumber());				//物料 数量
		map.put("materielOccupyNumber", w.getMaterielOccupyNumber());		//物料被占用的数量
		map.put("materielWarnAmount", w.getMaterielWarnAmount());			//预警值
		
		List<ErpCustomerWarehouse> cwh = erpCustomerWarehouseMapper.selectBatchIds(warehouseIds);
		String WarId = "-1";
		for(ErpCustomerWarehouse c:cwh){
			WarId +=","+c.getId();
		}
		
		//期初物料
		List<Map<String, Object>> mfslistW = erpMaterielMapper.selectByMaterielQiChu( WarId, startTime, w.getMaterielName(), w.getMaterielBrand(),
			w.getMaterielSpecifications(), w.getMaterielModel(), w.getMaterielPlace(),
			w.getMaterielState(), w.getMaterielUnit());
		
		//出入库
		List<Map<String, Object>> mfslist0W = erpMaterielMapper.selectByMaterielChuRu( WarId, startTime, endTime, w.getMaterielName(), w.getMaterielBrand(),
				w.getMaterielSpecifications(), w.getMaterielModel(), w.getMaterielPlace(),
				w.getMaterielState(), w.getMaterielUnit());
		
		DecimalFormat df = new DecimalFormat("######0.00");
		List<String> Fenleis = new ArrayList<String>();
		List<String[]> retuDatas = new ArrayList<String[]>();
		for(int i = 0; i < mfslistW.size(); i++){
			String[] retuData = new String[9];
			Map<String, Object> QC = mfslistW.get(i);
			String QCs = QC.get("num").toString();
			int Qc = Integer.parseInt(QCs);
			retuData[0] = QC.get("materiel_id").toString();
			retuData[1] = Qc < 0 ? "0" : QCs;
			retuData[2] = "0";
			retuData[3] = "0";
			retuData[4] = Qc < 0 ? "0" : QCs;
			retuData[5] = QC.get("cus_name").toString();
			retuData[6] = QC.get("war_name").toString();
			retuData[7] = "0";
			retuData[8] = "0";
			retuDatas.add(retuData);
			Fenleis.add(QC.get("war_name").toString());
		}
		
		for(int i = 0; i < mfslist0W.size(); i++){
			Map<String, Object> CRK =  mfslist0W.get(i);
			String Rk = CRK.get("rk").toString();
			String Ck = CRK.get("ck").toString();
			String Fenlei = CRK.get("war_name").toString();
			int index = Fenleis.indexOf(Fenlei);
			if(index == -1){
				String[] retuData = new String[9];
				retuData[0] = CRK.get("materiel_id").toString();
				retuData[1] = "0";
				retuData[2] = Rk;
				retuData[3] = Ck;
				int Qm = Integer.parseInt(Rk) - Integer.parseInt(Ck);
				retuData[4] = Qm < 0 ? "0" : Qm + "";
				retuData[5] = CRK.get("cus_name").toString();
				retuData[6] = CRK.get("war_name").toString();
				retuData[7] = "0";
				retuData[8] = "0";
				retuDatas.add(retuData);
			}else{
				String[] retuData = retuDatas.get(index);
				retuData[2] = Rk;
				retuData[3] = Ck;
				retuData[4] = Integer.parseInt(retuData[1]) + Integer.parseInt(Rk) - Integer.parseInt(Ck) + "";
				retuDatas.set(index, retuData);
			}
		}
		
		for(int i=0;i<retuDatas.size(); i++){
			String[] retuData = retuDatas.get(i);
			List<ErpWarehouse> ws = erpWarehouseMapper.selectList(new EntityWrapper<ErpWarehouse>().eq("id", Integer.valueOf(retuData[0])));
			Double materielTax = 0.00;
			Double materielNotax = 0.00;
			for(ErpWarehouse w1:ws){
				materielTax += w1.getMaterielTax();
				materielNotax += w1.getMaterielNotax();
			}
			retuData[7] = df.format(materielNotax/ws.size());
			retuData[8] = df.format(materielTax/ws.size());
			
			retuDatas.set(i, retuData);
		}
		map.put("data", retuDatas);
		return new Result<Map<String,Object>>(map, 1);
	}

	@Override
	public Result<List<String[]>> selectByBranchWarn(Integer warehouseIds, String startTime, String endTime) {
		// TODO Auto-generated method stub
		//期初物料
		List<Map<String, Object>> mfslistW = erpMaterielMapper.selectByWareIdsAndName(warehouseIds+"", "", startTime);

		// 出入库
		List<Map<String, Object>> mfslist0W = erpMaterielMapper.selectByWareIdsAndTime(warehouseIds+"", "", startTime, endTime);

		DecimalFormat df = new DecimalFormat("######0.00");
		List<String[]> retuDatas = new ArrayList<String[]>();
		List<String> Fenleis = new ArrayList<String>();
		for (int i = 0; i < mfslistW.size(); i++) {
			String[] retuData = new String[16];
			Map<String, Object> QC =  mfslistW.get(i);
			if (null != QC.get("materiel_name")) {
				String Pm = QC.get("materiel_name").toString();
				String Pp = QC.get("materiel_brand").toString();
				String Gg = QC.get("materiel_specifications").toString();
				String Xh = QC.get("materiel_model").toString();
				String Cd = QC.get("materiel_place").toString();
				String Dw = QC.get("materiel_unit") == null ? "" : QC.get("materiel_unit").toString();
				String ZT = QC.get("materiel_state").toString();
				String QCs = QC.get("num").toString();
				String YJ=QC.get("materiel_warn_amount").toString();         //预警值
				retuData[0] = QC.get("materiel_id").toString();
				retuData[1] = Pm;
				retuData[2] = Pp;
				retuData[3] = Gg;
				retuData[4] = Xh;
				retuData[5] = Cd;
				retuData[6] = ZT;
				int Qc = Integer.parseInt(QCs);
				retuData[7] = Qc < 0 ? "0" : QCs;
				retuData[8] = "0";
				retuData[9] = "0";
				retuData[10] = Qc < 0 ? "0" : QCs;
				retuData[11] = Dw;
				retuData[12] = QC.get("materiel_warn_amount") == null || QC.get("materiel_warn_amount").toString().equals("0") ? "" : QC.get("materiel_warn_amount").toString();
				retuData[13] = "0";
				retuData[14] = "0";
				retuData[15] = YJ;
				retuDatas.add(retuData);
				Fenleis.add(Pm + Pp + Gg + Xh + Cd + Dw + ZT);
			}
		}


		for (Map<String, Object> CRK:mfslist0W) {
			String Rk ="0";
			if(CRK.get("rk")!=null) {
				Rk= CRK.get("rk").toString();
			}
					
			String Ck = "0";
			if(CRK.get("ck")!=null) {
				Ck=CRK.get("ck").toString();
			}
			String Pm = CRK.get("materiel_name").toString();
			String Pp = CRK.get("materiel_brand").toString();
			String Gg = CRK.get("materiel_specifications").toString();
			String Xh = CRK.get("materiel_model").toString();
			String Cd = CRK.get("materiel_place").toString();
			String Dw = CRK.get("materiel_unit") == null || CRK.get("materiel_unit").toString().equals("0") ? "" : CRK.get("materiel_unit").toString();
			String ZT = CRK.get("materiel_state").toString();
			String YJ=CRK.get("materiel_warn_amount").toString();         //预警值
			String Fenlei = Pm + Pp + Gg + Xh + Cd + Dw + ZT;
			int index = Fenleis.indexOf(Fenlei);
			if (index == -1) {
				String[] retuData = new String[16];
				retuData[0] = CRK.get("materiel_id").toString();
				retuData[1] = Pm;
				retuData[2] = Pp;
				retuData[3] = Gg;
				retuData[4] = Xh;
				retuData[5] = Cd;
				retuData[6] = ZT;
				retuData[7] = "0";
				retuData[8] = Rk;
				retuData[9] = Ck;
				int Qm = Integer.parseInt(Rk) - Integer.parseInt(Ck);
				retuData[10] = Qm < 0 ? "0" : Qm + "";
				retuData[11] = Dw;
				retuData[12] = CRK.get("materiel_warn_amount") == null || CRK.get("materiel_warn_amount").toString().equals("0") ? "" : CRK.get("materiel_warn_amount").toString();
				retuData[13] = "0";
				retuData[14] = "0";
				retuData[15] = YJ;
				retuDatas.add(retuData);
			} else {
				String[] retuData = retuDatas.get(index);
				retuData[8] = Rk;
				retuData[9] = Ck;
				retuData[10] = Integer.parseInt(retuData[7])
						+ Integer.parseInt(Rk) - Integer.parseInt(Ck) + "";
				retuData[12] = CRK.get("materiel_warn_amount") == null ? "" : CRK.get("materiel_warn_amount").toString();
				retuDatas.set(index, retuData);
			}
		}
		List<String[]> newRetuDatas = new ArrayList<String[]>();
			
		for(int i = 0; i<retuDatas.size(); i++){
			String[] retuData = retuDatas.get(i);
			List<ErpWarehouse> ws = erpWarehouseMapper.selectByWareIds(retuData[1], retuData[2], retuData[3], retuData[4], retuData[5], retuData[6], retuData[11],warehouseIds+"");
			Double materielTax = 0.00;
			Double materielNotax = 0.00;
			for(ErpWarehouse w:ws){
				materielTax += w.getMaterielTax();
				materielNotax += w.getMaterielNotax();
			}
			if(ws.size() > 0){
				retuData[13] = df.format(materielNotax/ws.size());
				retuData[14] = df.format(materielTax/ws.size());
			}else{
				retuData[13] = df.format(0.00);
				retuData[14] = df.format(0.00);
			}
			newRetuDatas.add(retuData);
		}
		return new Result<List<String[]>>(newRetuDatas, newRetuDatas.size());
	}

	@Override
	public Result<Integer> shezhiWarn(int materielId, int custId, int wareId,int materielWarnAmount) {
		// TODO Auto-generated method stub
		try {
			List<ErpWarehouse> wareList=erpWarehouseMapper.selectList(new EntityWrapper<ErpWarehouse>().eq("id", materielId).eq("customer_id", custId).eq("warehouse_id", wareId));
			if(wareList.size()>0) {
				ErpWarehouse ware=wareList.get(0);
				ware.setMaterielWarnAmount(materielWarnAmount);
				int count=erpWarehouseMapper.updateAllColumnById(ware);
				return new Result<Integer>(count, 1);
			}else {
				return new Result<>(1, "数据异常");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}

	@Override
	public Result<List<Map<String, Object>>> selectDateils(String warehouseIds, int wareId, String startTime,
			String endTime) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list=new ArrayList<>();
		ErpWarehouse house=erpWarehouseMapper.selectById(wareId);
		List<ErpMateriel> matList=erpMaterielMapper.selectWareHouseDateils(warehouseIds, house.getMaterielName(), house.getMaterielBrand(), house.getMaterielSpecifications(), house.getMaterielModel(), house.getMaterielPlace(), house.getMaterielState(), house.getMaterielUnit(), startTime, endTime);
		for(ErpMateriel mat:matList) {
			ErpCustomer cust=erpCustomerMapper.selectById(mat.getCustomerId());
			ErpCustomerWarehouse ware=erpCustomerWarehouseMapper.selectById(mat.getWarehouseId());
			Map<String, Object> map=new HashMap<>();
			map.put("name", mat.getMaterielName());
			map.put("brand", mat.getMaterielBrand());
			map.put("specifications", mat.getMaterielSpecifications());
			map.put("model", mat.getMaterielModel());
			map.put("productionId", mat.getMaterielProductionId());
			map.put("eqcode", mat.getMaterielEqcode());
			map.put("rfid", mat.getMaterielRfid());
			map.put("storageDate", mat.getMaterielStorageDate());
			map.put("useDate", "");
			map.put("state", mat.getMaterielState());
			int IncludingTax=1;
			if(mat.getMaterielInvoiceType()!=null && mat.getMaterielInvoiceType()==2){
				IncludingTax = 0;    //含税
			}
			if(IncludingTax == 0){              //计算含税和未含税单价
				Double price = mat.getMaterielStoragePrice()*0.8547;
				map.put("hanPrice", price);   //*0.8547  计算含税和未含税单价
			}else{
				map.put("hanPrice", mat.getMaterielStoragePrice());   //*0.8547  计算含税和未含税单价
			}
			
			map.put("buHanPrice", mat.getMaterielStoragePrice());
			List<ErpInspectionContent> content=erpInspectionContentMapper.selectList(new EntityWrapper<ErpInspectionContent>().eq("tire_id", mat.getId()).orderBy("id", false));
			if(content!=null&&content.size()>0) {
				ErpInspectionContent ic=content.get(0);
				map.put("tire_tread1", ic.getDeepLines1());
				map.put("tire_tread2", ic.getDeepLines2());
				map.put("tire_tread3", ic.getDeepLines3());
				map.put("tire_tread4", ic.getDeepLines4());
			}else {
				ErpTireArchives tire=erpTireArchivesMapper.selectByTireId(mat.getId());
				if(tire!=null) {
					map.put("tire_tread1", tire.getTireTread1());
					map.put("tire_tread2", tire.getTireTread2());
					map.put("tire_tread3", tire.getTireTread3());
					map.put("tire_tread4", tire.getTireTread4());
				}else {
					map.put("tire_tread1", "");
					map.put("tire_tread2", "");
					map.put("tire_tread3", "");
					map.put("tire_tread4", "");
				}
			}
			map.put("cusName", cust.getCusName());
			map.put("wareHouse", ware.getWarName());
			list.add(map);
		}
		return new Result<List<Map<String,Object>>>(list, list.size());
	}

}
